/**
 * Create on 2010-4-22 04:13:06
 * @author JIXF
 * @E-Mail:jixf@mochasoft.com.cn
 */

package com.lq.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * <a href="PropertiesUtil.java.html"><b><i>View Source</i></b></a>
 * 
 * @author JIXF
 * 
 */
public class PropertiesUtil {

	public static Properties load(String s) throws IOException {
		Properties coreProp = new Properties();
		load(coreProp, s);
		return coreProp;
	}

	public static void load(Properties p, String s) throws IOException {
		if (Validator.isNotNull(s)) {
			InputStream fis = null;
			try {
				fis = getResourceAsStream(s);
				p.load(fis);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (fis != null) {
					try {
						fis.close();
					} catch (Exception e) {
						System.err.println(e.getMessage());
					}
				}
			}
		}
	}

	public static void copyProperties(Properties from, Properties to) {
		Iterator itr = from.entrySet().iterator();

		while (itr.hasNext()) {
			Map.Entry entry = (Map.Entry) itr.next();

			to.setProperty((String) entry.getKey(), (String) entry.getValue());
		}
	}

	public static Properties fromMap(Map map) {
		Properties p = new Properties();

		Iterator itr = map.entrySet().iterator();

		while (itr.hasNext()) {
			Map.Entry entry = (Map.Entry) itr.next();

			p.setProperty((String) entry.getKey(), (String) entry.getValue());
		}

		return p;
	}

	public static void fromProperties(Properties p, Map map) {
		map.clear();

		Iterator itr = p.entrySet().iterator();

		while (itr.hasNext()) {
			Map.Entry entry = (Map.Entry) itr.next();

			map.put(entry.getKey(), entry.getValue());
		}
	}

	public static InputStream getResourceAsStream(String resource)
			throws IOException {
		InputStream in = SystemProperties.class.getClassLoader()
				.getResourceAsStream(resource);
		if (in == null)
			in = ClassLoader.getSystemResourceAsStream(resource);
		if (in == null)
			throw new IOException("Could not find resource " + resource);
		return in;
	}

	public static void merge(Properties p1, Properties p2) {
		Enumeration enu = p2.propertyNames();

		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			String value = p2.getProperty(key);

			p1.setProperty(key, value);
		}
	}

	public static String toString(Properties p) {
		StringBuffer sb = new StringBuffer();

		Enumeration enu = p.propertyNames();

		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();

			sb.append(key);
			sb.append(StringPool.EQUAL);
			sb.append(p.getProperty(key));
			sb.append("\n");
		}

		return sb.toString();
	}

	public static void trimKeys(Properties p) {
		Enumeration enu = p.propertyNames();

		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			String value = p.getProperty(key);

			String trimmedKey = key.trim();

			if (!key.equals(trimmedKey)) {
				p.remove(key);
				p.setProperty(trimmedKey, value);
			}
		}
	}

	public static void main(String args[]) throws IOException {
		Properties p = PropertiesUtil.load("resourceConfig.properties");
		System.out.println(p.getProperty("ldap.port"));
	}

}